<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Word文档转PDF工具</title>
    <!-- 使用CDN加速和异步加载 -->
    <link rel="preconnect" href="https://cdn.jsdelivr.net">
    <link rel="dns-prefetch" href="https://cdn.jsdelivr.net">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-gradient: linear-gradient(135deg, #6e8efb, #4776E6);
            --secondary-gradient: linear-gradient(135deg, #8E2DE2, #4A00E0);
            --bg-gradient: linear-gradient(135deg, #E3F2FD, #BBDEFB);
            --card-bg: rgba(255, 255, 255, 0.95);
            --text-color: #2c3e50;
            --border-color: rgba(0, 0, 0, 0.1);
        }

        [data-theme="dark"] {
            --bg-gradient: linear-gradient(135deg, #1a1a2e, #16213e);
            --card-bg: rgba(30, 41, 59, 0.95);
            --text-color: #e2e8f0;
            --border-color: rgba(255, 255, 255, 0.1);
        }

        body {
            background: var(--bg-gradient);
            color: var(--text-color);
            min-height: 100vh;
            transition: all 0.3s ease;
        }

        .theme-switch {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 1000;
            background: var(--card-bg);
            border-radius: 50%;
            width: 45px;
            height: 45px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            border: 1px solid var(--border-color);
            transition: all 0.3s ease;
        }

        .theme-switch:hover {
            transform: scale(1.1);
            box-shadow: 0 4px 15px rgba(0,0,0,0.2);
        }

        .theme-switch i {
            font-size: 1.8rem;
            color: var(--text-color);
            transition: all 0.3s ease;
        }

        [data-theme="dark"] .theme-switch i.bi-sun-fill {
            color: #ffd700;
        }

        [data-theme="light"] .theme-switch i.bi-moon-stars-fill {
            color: #4776E6;
        }

        .upload-container {
            max-width: 700px;
            min-height: 400px;
            margin: 50px auto;
            padding: 30px;
            border-radius: 20px;
            background: var(--card-bg);
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            backdrop-filter: blur(10px);
            border: 1px solid var(--border-color);
            display: flex;
            flex-direction: column;
        }

        .upload-form-container {
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        .drag-area {
            border: 2px dashed var(--border-color);
            padding: 40px;
            text-align: center;
            margin: 20px 0;
            border-radius: 15px;
            cursor: pointer;
            transition: all 0.3s ease;
            background: rgba(255,255,255,0.05);
        }

        .drag-area.active {
            border-color: #4776E6;
            background: rgba(71, 118, 230, 0.05);
        }

        .btn-primary {
            background: var(--primary-gradient);
            border: none;
            padding: 10px 25px;
            border-radius: 10px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(71, 118, 230, 0.2);
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(71, 118, 230, 0.3);
        }

        .btn-secondary {
            background: var(--secondary-gradient);
            border: none;
            padding: 10px 25px;
            border-radius: 10px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(142, 45, 226, 0.2);
        }

        .btn-secondary:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(142, 45, 226, 0.3);
        }

        .file-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 12px 15px;
            border: 1px solid var(--border-color);
            margin-bottom: 8px;
            border-radius: 10px;
            background: rgba(255,255,255,0.05);
            backdrop-filter: blur(5px);
        }

        .progress-container {
            margin-top: 30px;
        }

        .progress {
            height: 12px;
            border-radius: 6px;
            background: rgba(0,0,0,0.05);
            overflow: hidden;
        }

        .progress-bar {
            background: var(--primary-gradient);
            transition: width 0.3s ease;
        }

        .stage-indicator {
            margin-top: 20px;
            padding: 15px;
            background: rgba(255,255,255,0.05);
            border-radius: 15px;
            border: 1px solid var(--border-color);
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: relative;
        }

        .stage {
            font-size: 0.9rem;
            font-weight: 500;
            text-align: center;
            position: relative;
            z-index: 2;
            padding: 10px;
            flex: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 5px;
        }

        .stage::before {
            content: '';
            width: 30px;
            height: 30px;
            background: var(--card-bg);
            border: 2px solid var(--border-color);
            border-radius: 50%;
            margin-bottom: 5px;
            transition: all 0.3s ease;
        }

        .stage::after {
            content: '';
            position: absolute;
            top: 24px;
            right: -50%;
            width: 100%;
            height: 2px;
            background: var(--border-color);
            z-index: 1;
        }

        .stage:last-child::after {
            display: none;
        }

        .stage.active::before {
            background: var(--primary-gradient);
            border-color: transparent;
        }

        .stage.active {
            color: #4776E6;
        }

        .stage.active ~ .stage::after {
            background: var(--border-color);
        }

        .stage.active::after {
            background: var(--primary-gradient);
        }

        .stage.completed::before {
            background: var(--primary-gradient);
            border-color: transparent;
        }

        .stage.completed::after {
            background: var(--primary-gradient);
        }

        h2 {
            background: var(--primary-gradient);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 700;
            margin-bottom: 30px;
        }

        .alert {
            border-radius: 10px;
            padding: 15px 20px;
        }

        .alert-success {
            background: linear-gradient(135deg, #43A047, #66BB6A);
            border: none;
            color: white;
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 15px;
        }

        .alert-success .btn-group {
            display: flex;
            gap: 10px;
            flex-shrink: 0;
        }

        .alert-success .btn {
            padding: 8px 20px;
            background: rgba(255, 255, 255, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            font-weight: 500;
            transition: all 0.3s ease;
            margin: 0;
        }

        .alert-success .btn:hover {
            background: rgba(255, 255, 255, 0.3);
            transform: translateY(-1px);
        }

        .success-message {
            margin: 0;
            flex-grow: 1;
        }

        @keyframes float {
            0% { transform: translateY(0px); }
            50% { transform: translateY(-5px); }
            100% { transform: translateY(0px); }
        }

        .drag-area:hover .icon {
            animation: float 2s ease-in-out infinite;
        }

        .drag-area .icon {
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .drag-area .icon i {
            transition: all 0.3s ease;
        }

        .drag-area:hover .icon i {
            transform: scale(1.1) rotate(5deg);
        }

        .drag-area:hover .icon i:last-child {
            transform: scale(1.1) rotate(-5deg);
        }

        [data-theme="dark"] .drag-area .icon i:first-child {
            color: #e2e8f0;
        }

        [data-theme="dark"] .drag-area .icon i:last-child {
            color: #4776E6;
        }

        .preview-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.8);
            z-index: 1000;
            justify-content: center;
            align-items: center;
        }

        .preview-modal.active {
            display: flex;
        }

        .preview-content {
            width: 90%;
            height: 90%;
            background: white;
            border-radius: 15px;
            position: relative;
            overflow: hidden;
            display: flex;
            flex-direction: column;
        }

        .preview-header {
            padding: 15px;
            background: var(--primary-gradient);
            color: white;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .preview-close {
            background: none;
            border: none;
            color: white;
            font-size: 1.5rem;
            cursor: pointer;
            padding: 5px;
            transition: transform 0.3s ease;
        }

        .preview-close:hover {
            transform: scale(1.1);
        }

        .preview-iframe {
            flex: 1;
            width: 100%;
            border: none;
        }
    </style>
</head>
<body>
    <div class="theme-switch" onclick="toggleTheme()" title="切换主题">
        <i class="fas fa-sun"></i>
    </div>

    <!-- 添加预览模态框 -->
    <div class="preview-modal" id="previewModal">
        <div class="preview-content">
            <div class="preview-header">
                <h5 class="m-0">PDF 预览</h5>
                <button class="preview-close" onclick="closePreview()">&times;</button>
            </div>
            <iframe class="preview-iframe" id="previewFrame"></iframe>
        </div>
    </div>

    <div class="container">
        <div class="upload-container bg-white">
            <h2 class="text-center mb-4">Word文档转PDF工具</h2>
            
            <div th:if="${message}" class="alert alert-success" role="alert">
                <span th:text="${message}"></span>
                <a th:if="${outputPath}" th:href="@{'/download/' + ${outputPath}}" 
                   class="btn btn-primary btn-sm float-end">
                    下载PDF
                </a>
            </div>
            
            <div th:if="${error}" class="alert alert-danger" role="alert" th:text="${error}"></div>

            <div class="upload-form-container">
                <form method="POST" action="/upload" enctype="multipart/form-data" id="upload-form">
                    <div class="drag-area" id="drag-area">
                        <div class="text">拖放文件到这里或点击选择文件</div>
                        <input type="file" name="files" multiple accept=".doc,.docx" 
                               style="display: none" id="file-input">
                    </div>

                    <div id="file-list" class="mb-3"></div>

                    <div class="progress-container" id="progress-container" style="display: none;">
                        <div class="overall-progress">
                            <h6 class="mb-2">总体进度</h6>
                            <div class="progress">
                                <div class="progress-bar progress-bar-striped progress-bar-animated bg-success" 
                                     role="progressbar" style="width: 0%" id="overall-progress-bar">0%</div>
                            </div>
                            <div class="stage-indicator">
                                <div class="stage" data-stage="upload">
                                    <span>上传文件</span>
                                </div>
                                <div class="stage" data-stage="convert">
                                    <span>转换处理</span>
                                </div>
                                <div class="stage" data-stage="merge">
                                    <span>合并PDF</span>
                                </div>
                                <div class="stage" data-stage="complete">
                                    <span>完成</span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="text-center mt-3">
                        <button type="submit" class="btn btn-primary" id="submit-btn" disabled>
                            开始转换
                        </button>
                        <button type="button" class="btn btn-secondary ms-2" id="reset-btn">
                            重置
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        // 在现有的立即执行函数前添加主题切换功能
        function toggleTheme() {
            const body = document.body;
            const icon = document.querySelector('.theme-switch i');
            
            if (body.getAttribute('data-theme') === 'dark') {
                body.removeAttribute('data-theme');
                icon.className = 'fas fa-sun';
                localStorage.setItem('theme', 'light');
            } else {
                body.setAttribute('data-theme', 'dark');
                icon.className = 'fas fa-moon';
                localStorage.setItem('theme', 'dark');
            }
        }

        // 页面加载时检查主题
        document.addEventListener('DOMContentLoaded', () => {
            const savedTheme = localStorage.getItem('theme') || 'light';
            const icon = document.querySelector('.theme-switch i');
            if (savedTheme === 'dark') {
                document.body.setAttribute('data-theme', 'dark');
                icon.className = 'fas fa-moon';
            } else {
                icon.className = 'fas fa-sun';
            }
        });

        // 使用立即执行函数避免全局变量污染
        (function() {
            const dragArea = document.getElementById('drag-area');
            const fileInput = document.getElementById('file-input');
            const fileList = document.getElementById('file-list');
            const submitBtn = document.getElementById('submit-btn');
            const form = document.getElementById('upload-form');
            const progressContainer = document.getElementById('progress-container');
            const overallProgressBar = document.getElementById('overall-progress-bar');
            let files = [];

            // 使用事件委托优化事件处理
            dragArea.addEventListener('click', () => fileInput.click());
            fileInput.addEventListener('change', () => handleFiles(fileInput.files));

            dragArea.addEventListener('dragover', (e) => {
                e.preventDefault();
                dragArea.classList.add('active');
            });

            dragArea.addEventListener('dragleave', () => {
                dragArea.classList.remove('active');
            });

            dragArea.addEventListener('drop', (e) => {
                e.preventDefault();
                dragArea.classList.remove('active');
                handleFiles(e.dataTransfer.files);
            });

            function handleFiles(fileList) {
                for (let file of fileList) {
                    if (file.name.match(/\.(doc|docx)$/i) && !files.some(f => f.name === file.name)) {
                        files.push(file);
                        displayFile(file);
                    }
                }
                updateSubmitButton();
                updateFileInput();
            }

            function displayFile(file) {
                const div = document.createElement('div');
                div.className = 'file-item';
                div.innerHTML = `
                    <span>${file.name}</span>
                    <span class="remove-file" data-filename="${file.name}">&times;</span>
                `;
                fileList.appendChild(div);
            }

            // 使用事件委托处理文件删除
            fileList.addEventListener('click', (e) => {
                if (e.target.classList.contains('remove-file')) {
                    const fileName = e.target.dataset.filename;
                    files = files.filter(f => f.name !== fileName);
                    updateFileList();
                    updateSubmitButton();
                    updateFileInput();
                }
            });

            function updateFileList() {
                fileList.innerHTML = '';
                files.forEach(displayFile);
            }

            function updateSubmitButton() {
                submitBtn.disabled = files.length === 0;
            }

            function updateFileInput() {
                const dataTransfer = new DataTransfer();
                files.forEach(file => dataTransfer.items.add(file));
                fileInput.files = dataTransfer.files;
            }

            function updateStage(stage) {
                const stages = document.querySelectorAll('.stage');
                let found = false;
                
                stages.forEach(el => {
                    el.classList.remove('active');
                    if (el.dataset.stage === stage) {
                        el.classList.add('active');
                        found = true;
                    }
                    // 如果已经找到当前阶段，之前的阶段都标记为已完成
                    if (!found) {
                        el.classList.add('completed');
                    } else {
                        el.classList.remove('completed');
                    }
                });
            }

            form.addEventListener('submit', async (e) => {
                e.preventDefault();
                if (files.length === 0) return;

                submitBtn.disabled = true;
                progressContainer.style.display = 'block';
                
                const formData = new FormData();
                files.forEach(file => formData.append('files', file));

                try {
                    updateStage('upload');
                    let progress = 0;
                    
                    // 模拟总体进度
                    const progressInterval = setInterval(() => {
                        if (progress < 100) {
                            progress += 2;
                            overallProgressBar.style.width = `${progress}%`;
                            overallProgressBar.textContent = `${progress}%`;
                            
                            if (progress < 33) {
                                updateStage('upload');
                            } else if (progress < 66) {
                                updateStage('convert');
                            } else if (progress < 100) {
                                updateStage('merge');
                            }
                        } else {
                            clearInterval(progressInterval);
                        }
                    }, 200);

                    const response = await fetch('/upload', {
                        method: 'POST',
                        body: formData
                    });

                    if (response.ok) {
                        const result = await response.json();
                        clearInterval(progressInterval);
                        updateStage('complete');
                        overallProgressBar.style.width = '100%';
                        overallProgressBar.textContent = '100%';
                        
                        // 更新成功提示，添加预览按钮
                        const successAlert = document.createElement('div');
                        successAlert.className = 'alert alert-success mt-3';
                        successAlert.innerHTML = `
                            <span class="success-message">文件转换成功！</span>
                            <div class="btn-group">
                                <button class="btn" onclick="openPreview('/preview/${result.outputPath}')">
                                    预览
                                </button>
                                <a href="/download/${result.outputPath}" class="btn">
                                    下载PDF
                                </a>
                            </div>
                        `;
                        
                        // 插入到进度条下方
                        progressContainer.appendChild(successAlert);
                        
                        // 重置按钮状态，允许继续转换其他文件
                        submitBtn.disabled = false;
                        files = [];
                        updateFileList();
                        updateFileInput();
                    } else {
                        throw new Error('上传失败');
                    }
                } catch (error) {
                    overallProgressBar.className = 'progress-bar bg-danger';
                    overallProgressBar.textContent = '处理失败';
                    submitBtn.disabled = false;
                }
            });

            function resetForm() {
                files = [];
                fileList.innerHTML = '';
                fileInput.value = '';
                progressContainer.style.display = 'none';
                submitBtn.disabled = true;
                // 移除之前的成功提示（如果存在）
                const successAlert = progressContainer.querySelector('.alert-success');
                if (successAlert) {
                    successAlert.remove();
                }
                // 重置进度条
                overallProgressBar.style.width = '0%';
                overallProgressBar.textContent = '0%';
                overallProgressBar.className = 'progress-bar progress-bar-striped progress-bar-animated bg-success';
                // 重置所有阶段指示器
                document.querySelectorAll('.stage').forEach(el => {
                    el.classList.remove('active', 'completed');
                });
            }

            // 添加重置按钮事件监听器
            document.getElementById('reset-btn').addEventListener('click', resetForm);
        })();

        // 添加预览相关函数
        function openPreview(url) {
            const modal = document.getElementById('previewModal');
            const iframe = document.getElementById('previewFrame');
            iframe.src = url;
            modal.classList.add('active');
            document.body.style.overflow = 'hidden';
        }

        function closePreview() {
            const modal = document.getElementById('previewModal');
            const iframe = document.getElementById('previewFrame');
            iframe.src = '';
            modal.classList.remove('active');
            document.body.style.overflow = '';
        }

        // 点击模态框背景关闭预览
        document.getElementById('previewModal').addEventListener('click', (e) => {
            if (e.target.classList.contains('preview-modal')) {
                closePreview();
            }
        });
    </script>
</body>
</html> 